home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
104_01
/
c1.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
10KB
|
470 lines
/* >>>>> start ccl <<<<<< */
/* */
/* Compiler begins execution here */
/* */
#ifndef TRUE /*see if need to use include file */
#include <c.def>
#endif
main()
{
glbptr=startglb; /* clear global symbols */
locptr=startloc; /* clear local symbols */
wqptr=wq; /* clear while queue */
macptr= /* clear the macro pool */
litptr= /* clear literal pool */
nxtlab= /* next label to assign */
sp = /* stackptr (relative) */
errcnt= /* no errors */
eof= /* not eof yet */
input= /* no input file */
input2= /* or include file */
output= /* no open units */
ncmp= /* no open compound states */
ctext= /* do not print text */
lastst=0; /* no last statement yet */
cif= /* state of #ifxxxx */
cmode=1; /* enable preprocessing */
litlab=getlabel(); /* label number for literal poll */
/* */
/* compiler body */
/* */
ask(); /* get user options */
openout(); /* get an output file */
openin(); /* and initial input file */
header(); /* intro code */
parse(); /* process ALL input */
dumplits(); /* then dump literal pool */
dumpglbs(); /* and all static memory */
dumpextrn(); /* make all function defined extrn */
errorsummary(); /* summarize errors */
trailer(); /* follow-up code */
closeout(); /* close the output */
if (errcnt) unlink("a:$$$.sub"); /* if errors erase submit file */
return; /* then exit to system */
}
/* */
/* Process all input text */
/* */
/* At this level, only static declarations, */
/* defines, includes, and function */
/* definitions are legal... */
parse()
{
while (!eof) { /* do until no more input */
if(amatch("char",4)) {
declglb(cchar);
ns();
}
else if(amatch("int",3)) {
declglb(cint);
ns();
}
else if(match("#asm")) doasm();
else if(match("#include")) doinclude();
else if(match("#define")) addmac();
else if(match("#ifndef")) doifndef();
else if(match("#ifdef")) doifdef();
else newfunc();
blanks(); /* force eof if pending */
}
}
/* */
/* Dump the literal pool */
/* */
dumplits()
{
int j,k;
if (litptr==0) return; /* if nothing there, exit...*/
printlabel(litlab);
col(); /* print literal label */
k=0; /* init an index... */
while (k<litptr) { /* to loop with */
defbyte(); /* pseudo-op to define byte */
j=10; /* max bytes per line */
while(j--) {
outdec(litq[k++]);
if (j==0 || k>=litptr) {
nl(); /* need <cr> */
break;
}
outbyte(','); /* separate bytes */
}
}
litptr=0;
}
/* */
/* Dump all static variables */
/* */
dumpglbs()
{
int j;
dsect("@globals");
cptr=startglb;
while(cptr<glbptr) {
if (cptr[ident] != function) {
/* do if anything but function */
outstr(cptr);col();
/* output name as label... */
defstorage(); /*define storage */
j=((cptr[offset]&255)+
((cptr[offset+1]&255)<<8));
/* calc # bytes */
outdec(j); /* need that many */
nl();
}
cptr=cptr+symsiz;
}
}
/* */
/* dump extrn function */
/* */
dumpextrn()
{
char *ptr;
ptr=startglb;
while(ptr!=glbptr) {
if (!ptr[offset]) extrn(ptr);
ptr=ptr+symsiz;
}
}
/* */
/* Report errors for user */
/* */
errorsummary()
{
/* see if anything left hanging... */
if (ncmp) error("missing closing bracket");
/* open compund statment ... */
if (!cif) error("missing closing #endif");
nl();
comment();
outdec(errcnt); /* total # errors */
outstr(" errors in compilation.");
nl();
}
/* */
/* Get options from user */
/* */
ask()
{
int k;
kill(); /* clear input line */
pl(" * * * small-c version 1.1 compiler * * *\n");
/* see if user wants to interleasve the c-text */
/* inform of comments (for clarity) */
pl("Do you wish to c-text to appear? ");
gets(line); /* get answer */
if(toupper(ch())=='Y') ctext=1; /* user said yes */
}
/* */
/* Get output filename */
/* */
openout()
{
kill(); /* erase line */
output=0; /* start with none */
pl("Output filename? "); /* ask...*/
gets(line); /* get a filename */
if(ch()==0)return; /* none given... */
if ((output=fcreat(line,fout)) == ERROR) {
output=0; /* can't open */
error("Open failure");
}
kill(); /* erase line */
}
/* */
/* Get (next) input file */
/* */
openin()
{
input=0; /* none to start with */
while (!input) /* any above 1 allowed */
{kill(); /* clear line */
if(eof)break; /* if user said none */
pl("Input filename? ");
gets(line); /* get a name */
if(ch()==0) {
eof=1;
break; /* none given... */
}
line1=0;
if ((input=fopen(line,finp)) == ERROR) {
input=0; /* can't open it */
pl("Open failure");
}
}
kill(); /* erase line */
}
/* */
/* Open an include file */
/* */
doinclude()
{
char fname[31]; /* place to open file name */
char fend; /* ending character for file name */
int lenght; /* size of file name */
while(isspace(ch())) gch(); /* skip over to name */
fend=' ';
if (ch()=='<') {
fend='>';
gch();
}
if (ch()=='\"') {
fend='\"';
gch();
}
lenght=0;
while(lenght<30 && ch() && ch()!=fend) {
fname[lenght++]=ch();
gch();
}
fname[lenght]=0;
if (!(fend == ch() || (fend ==' ' && !ch()))) {
error("invaild file name for include");
kill();
return;
}
if (!lenght) {
error("missing file name for include");
kill();
return;
}
line2=0;
if (input2) {
error("nested include not aloud");
kill();
return;
}
if ((input2=fopen(fname,finp2)) == ERROR) {
error("Open failure on include file");
input2=0;
}
kill();
}
/* */
/* #ifndef */
/* */
doifndef()
{
char sname[namesize];
int lval[2];
/* check to if define as constant */
blanks();
if (constant(lval)) {
cif=FALSE;
return;
}
/* check to see if vaild sysmbol */
if (!symname(sname)) {
error("invaild identifier");
kill();
return;
}
/* check to see if symbol is define */
if (findloc(sname) || findglb(sname))
cif=FALSE; else cif=TRUE;
}
/* */
/* #ifdef */
/* */
doifdef()
{
char sname[namesize];
int lval[2];
/* check to see if constant */
blanks();
if (!constant(lval)) {
cif=FALSE;
return;
}
/* check to see if vaild symbol */
if (!symname(sname)) {
error("invaild identifier");
kill();
}
/* check to see if symbol is define */
if (findloc(sname) || findglb(sname))
cif=TRUE; else cif=FALSE;
}
/* */
/* Close the output file */
/* */
closeout()
{
if (output) { /* if open mark end, close it */
putc(26,fout);
fflush(fout);
fclose(output);
}
output=0; /* mark as closed */
}
/* */
/* Declare a static variable */
/* (i.e. define for use) */
/* */
/* makes an entry in the symbol table so subsequent */
/* references can call symbol by name */
declglb(typ) /* typ is cchar or cint */
int typ;
{
int j;
int count;
char sname[namesize];
while(1) {
count=0;
if (endst()) return;
if(match("*")) {
j=pointer;
++count;
while(ch()=='*') {
++count;
gch();
}
}
else j=variable;
if (!symname(sname)) illname();
if(findglb(sname)) multidef(sname);
data_parse(sname,typ,statik,j,count);
if (match(",")==0) return; /* more? */
}
}
/* */
/* Declare local variables */
/* (i.e. define for use) */
/* */
/* works just like "declglb" but modifies machine stack */
/* and adds symbol table entry with appropriate */
/* stack offset to find it again */
declloc(typ) /* typ is cchar or cint */
int typ;
{
int j;
int count;
char sname[namesize];
while(1) {
count=0;
if (endst()) return;
if (match("*")) {
j=pointer;
++count;
while(ch()=='*') {
++count;
gch();
}
}
else j=variable;
if (!symname(sname)) illname();
if (findloc(sname)) multidef(sname);
data_parse(sname,typ,stkloc,j,count);
if (